<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Session;
class Base extends controller{  
    /**
     * 模块 
     */
    public $module;
    
    
    /**
     * 登录后记录的用户id
     */
    public $uid = '';

    /**
     * 登录后记录的用户信息
     */
    public $user_auth = [];
    
    /**
     * 用户名
     */
    public $username = '';
    
    /**
     * 用户昵称
     */
    public $nickname = '';
    
    /**
     * 获取控制器名称
     */
    public $controller = '';
    
    /**
     * 获取动作名称
     */
    public $action = '';
    
    /**
     * 授权后的菜单
     */
    public $menu = [];

    /**
     * 无需授权的方法
     */
    public $unauthorizedAction = [
        
    ];
    
    /**
     * 全局操作初始化方法
     */
    protected function initialize() {

        

        
        
        //获取session信息
        $this->user_auth = $user_auth = session( 'admin.user_auth' );
        
        //获取当前模块名称
        $this->module = request()->module();
        
        //获取当前控制器名称
        $this->controller = request()->controller();
        
        //获取当前动作名称
        $this->action = request()->action();
        
  
        
        //session 登录的管理员uid
        $this->uid = $user_auth['uid'];
        
        //管理员用户名
        $this->username = $user_auth['username'];
        
        //管理员用户昵称
        $this->nickname = $user_auth['nickname'];
        
        //实例授权auth模块
        $auth = new Auth();
        
        //获取访问的url地址
        $menu_path = $this->module. '/' . $this->controller . '/' . $this->action;
        if( strtolower( $this->controller ).'/'.strtolower( $this->action ) == 'index/index' ) {
            //index不做权限验证
        } else {
            //id为1的用户是超级管理员，不做权限验证
            if( 1 != intval( $this->uid ) ) {
                $this->unauthorizedAction = array_map( 'strtolower', $this->unauthorizedAction );
                if( !in_array( strtolower( $menu_path ), $this->unauthorizedAction) ) {
                    if( !$auth->check( $menu_path, $this->uid, ['in','1'] ) ) {
                        $this->error( '没有授权访问' );
                    }
                }
            }
        }
        
        //对模版输出全局静态文件根路径

        $this->assign( 'static', 'http://zaixianjiaoyu.com/static/'.$this->module );

        $this->assign( 'static', 'http://zaixianjiaoyu.com/static/'.$this->module );

        $this->top_menu = model( 'Menu' )->getTopMenus();

        //获取全局授权后的菜单
        $this->assign( 'top_menu', $this->top_menu );

        //所有左侧有权限的菜单
        $this->all_left_menu = model( 'Menu' )->getAllLeftMenu();
        $this->assign( 'all_left_menu', $this->all_left_menu );
    }

    

}